// Copyright (c) Lawrence Livermore National Security, LLC and other VisIt
// Project developers.  See the top-level LICENSE file for dates and other
// details.  No copyright assignment is required to contribute to VisIt.

#include <PySelectionSummary.h>
#include <ObserverToCallback.h>
#include <stdio.h>
#include <Py2and3Support.h>
#include <PySelectionVariableSummary.h>

// ****************************************************************************
// Module: PySelectionSummary
//
// Purpose:
//   Contains attributes for a selection
//
// Note:       Autogenerated by xml2python. Do not modify by hand!
//
// Programmer: xml2python
// Creation:   omitted
//
// ****************************************************************************

//
// This struct contains the Python type information and a SelectionSummary.
//
struct SelectionSummaryObject
{
    PyObject_HEAD
    SelectionSummary *data;
    bool        owns;
    PyObject   *parent;
};

//
// Internal prototypes
//
static PyObject *NewSelectionSummary(int);
std::string
PySelectionSummary_ToString(const SelectionSummary *atts, const char *prefix, const bool forLogging)
{
    std::string str;
    char tmpStr[1000];

    snprintf(tmpStr, 1000, "%sname = \"%s\"\n", prefix, atts->GetName().c_str());
    str += tmpStr;
    { // new scope
        int index = 0;
        // Create string representation of variables from atts.
        for(AttributeGroupVector::const_iterator pos = atts->GetVariables().begin(); pos != atts->GetVariables().end(); ++pos, ++index)
        {
            const SelectionVariableSummary *current = (const SelectionVariableSummary *)(*pos);
            snprintf(tmpStr, 1000, "GetVariables(%d).", index);
            std::string objPrefix(prefix + std::string(tmpStr));
            str += PySelectionVariableSummary_ToString(current, objPrefix.c_str(), forLogging);
        }
        if(index == 0)
            str += "#variables does not contain any SelectionVariableSummary objects.\n";
    }
    snprintf(tmpStr, 1000, "%scellCount = %d\n", prefix, atts->GetCellCount());
    str += tmpStr;
    snprintf(tmpStr, 1000, "%stotalCellCount = %d\n", prefix, atts->GetTotalCellCount());
    str += tmpStr;
    {   const doubleVector &histogramValues = atts->GetHistogramValues();
        snprintf(tmpStr, 1000, "%shistogramValues = (", prefix);
        str += tmpStr;
        for(size_t i = 0; i < histogramValues.size(); ++i)
        {
            snprintf(tmpStr, 1000, "%g", histogramValues[i]);
            str += tmpStr;
            if(i < histogramValues.size() - 1)
            {
                snprintf(tmpStr, 1000, ", ");
                str += tmpStr;
            }
        }
        snprintf(tmpStr, 1000, ")\n");
        str += tmpStr;
    }
    snprintf(tmpStr, 1000, "%shistogramMinBin = %g\n", prefix, atts->GetHistogramMinBin());
    str += tmpStr;
    snprintf(tmpStr, 1000, "%shistogramMaxBin = %g\n", prefix, atts->GetHistogramMaxBin());
    str += tmpStr;
    return str;
}

static PyObject *
SelectionSummary_Notify(PyObject *self, PyObject *args)
{
    SelectionSummaryObject *obj = (SelectionSummaryObject *)self;
    obj->data->Notify();
    Py_INCREF(Py_None);
    return Py_None;
}

/*static*/ PyObject *
SelectionSummary_SetName(PyObject *self, PyObject *args)
{
    SelectionSummaryObject *obj = (SelectionSummaryObject *)self;

    PyObject *packaged_args = 0;

    // Handle args packaged as first member of a tuple of size one
    // if we think the unpackaged args matches our needs
    if (PySequence_Check(args) && PySequence_Size(args) == 1)
    {
        packaged_args = PySequence_GetItem(args, 0);
        if (PyUnicode_Check(packaged_args))
            args = packaged_args;
    }

    if (!PyUnicode_Check(args))
    {
        Py_XDECREF(packaged_args);
        return PyErr_Format(PyExc_TypeError, "arg is not a unicode string");
    }

    char const *val = PyUnicode_AsUTF8(args);
    std::string cval = std::string(val);

    if (val == 0 && PyErr_Occurred())
    {
        Py_XDECREF(packaged_args);
        PyErr_Clear();
        return PyErr_Format(PyExc_TypeError, "arg not interpretable as utf8 string");
    }

    Py_XDECREF(packaged_args);

    // Set the name in the object.
    obj->data->SetName(cval);

    Py_INCREF(Py_None);
    return Py_None;
}

/*static*/ PyObject *
SelectionSummary_GetName(PyObject *self, PyObject *args)
{
    SelectionSummaryObject *obj = (SelectionSummaryObject *)self;
    PyObject *retval = PyString_FromString(obj->data->GetName().c_str());
    return retval;
}

/*static*/ PyObject *
SelectionSummary_GetVariables(PyObject *self, PyObject *args)
{
    SelectionSummaryObject *obj = (SelectionSummaryObject *)self;
    int index = -1;
    if (args == NULL)
        return PyErr_Format(PyExc_NameError, "Use .GetVariables(int index) to get a single entry");
    if (!PyArg_ParseTuple(args, "i", &index))
        return PyErr_Format(PyExc_TypeError, "arg must be a single integer index");
    if (index < 0 || (size_t)index >= obj->data->GetVariables().size())
        return PyErr_Format(PyExc_ValueError, "index out of range");

    // Since the new object will point to data owned by the this object,
    // we need to increment the reference count.
    Py_INCREF(self);

    PyObject *retval = PySelectionVariableSummary_Wrap(&obj->data->GetVariables(index));
    // Set the object's parent so the reference to the parent can be decref'd
    // when the child goes out of scope.
    PySelectionVariableSummary_SetParent(retval, self);

    return retval;
}

PyObject *
SelectionSummary_GetNumVariables(PyObject *self, PyObject *args)
{
    SelectionSummaryObject *obj = (SelectionSummaryObject *)self;
    return PyInt_FromLong((long)obj->data->GetVariables().size());
}

PyObject *
SelectionSummary_AddVariables(PyObject *self, PyObject *args)
{
    SelectionSummaryObject *obj = (SelectionSummaryObject *)self;
    PyObject *element = NULL;
    if(!PyArg_ParseTuple(args, "O", &element))
        return NULL;
    if(!PySelectionVariableSummary_Check(element))
        return PyErr_Format(PyExc_TypeError, "expected attr object of type SelectionVariableSummary");
    SelectionVariableSummary *newData = PySelectionVariableSummary_FromPyObject(element);
    obj->data->AddVariables(*newData);
    obj->data->SelectVariables();
    Py_INCREF(Py_None);
    return Py_None;
}

static PyObject *
SelectionSummary_Remove_One_Variables(PyObject *self, int index)
{
    SelectionSummaryObject *obj = (SelectionSummaryObject *)self;
    // Remove in the AttributeGroupVector instead of calling RemoveVariables() because we don't want to delete the object; just remove it.
    AttributeGroupVector &atts = obj->data->GetVariables();
    AttributeGroupVector::iterator pos = atts.begin();
    // Iterate through the vector "index" times.
    for(int i = 0; i < index; ++i)
        ++pos;

    // If pos is still a valid iterator, remove that element.
    if(pos != atts.end())
    {
        // NOTE: Leak the object since other Python objects may reference it. Ideally,
        // we would put the object into some type of pool to be cleaned up later but
        // this will do for now.
        //
        // delete *pos;
        atts.erase(pos);
    }

    obj->data->SelectVariables();
    Py_INCREF(Py_None);
    return Py_None;
}

PyObject *
SelectionSummary_RemoveVariables(PyObject *self, PyObject *args)
{
    int index = -1;
    if(!PyArg_ParseTuple(args, "i", &index))
        return PyErr_Format(PyExc_TypeError, "Expecting integer index");
    SelectionSummaryObject *obj = (SelectionSummaryObject *)self;
    if(index < 0 || index >= obj->data->GetNumVariables())
        return PyErr_Format(PyExc_IndexError, "Index out of range");

    return SelectionSummary_Remove_One_Variables(self, index);
}

PyObject *
SelectionSummary_ClearVariables(PyObject *self, PyObject *args)
{
    SelectionSummaryObject *obj = (SelectionSummaryObject *)self;
    int n = obj->data->GetNumVariables();
    for(int i = 0; i < n; ++i)
    {
        SelectionSummary_Remove_One_Variables(self, 0);
        Py_DECREF(Py_None);
    }
    Py_INCREF(Py_None);
    return Py_None;
}

/*static*/ PyObject *
SelectionSummary_SetCellCount(PyObject *self, PyObject *args)
{
    SelectionSummaryObject *obj = (SelectionSummaryObject *)self;

    PyObject *packaged_args = 0;

    // Handle args packaged into a tuple of size one
    // if we think the unpackaged args matches our needs
    if (PySequence_Check(args) && PySequence_Size(args) == 1)
    {
        packaged_args = PySequence_GetItem(args, 0);
        if (PyNumber_Check(packaged_args))
            args = packaged_args;
    }

    if (PySequence_Check(args))
    {
        Py_XDECREF(packaged_args);
        return PyErr_Format(PyExc_TypeError, "expecting a single number arg");
    }

    if (!PyNumber_Check(args))
    {
        Py_XDECREF(packaged_args);
        return PyErr_Format(PyExc_TypeError, "arg is not a number type");
    }

    long val = PyLong_AsLong(args);
    int cval = int(val);

    if (val == -1 && PyErr_Occurred())
    {
        Py_XDECREF(packaged_args);
        PyErr_Clear();
        return PyErr_Format(PyExc_TypeError, "arg not interpretable as C++ int");
    }
    if (fabs(double(val))>1.5E-7 && fabs((double(long(cval))-double(val))/double(val))>1.5E-7)
    {
        Py_XDECREF(packaged_args);
        return PyErr_Format(PyExc_ValueError, "arg not interpretable as C++ int");
    }

    Py_XDECREF(packaged_args);

    // Set the cellCount in the object.
    obj->data->SetCellCount(cval);

    Py_INCREF(Py_None);
    return Py_None;
}

/*static*/ PyObject *
SelectionSummary_GetCellCount(PyObject *self, PyObject *args)
{
    SelectionSummaryObject *obj = (SelectionSummaryObject *)self;
    PyObject *retval = PyInt_FromLong(long(obj->data->GetCellCount()));
    return retval;
}

/*static*/ PyObject *
SelectionSummary_SetTotalCellCount(PyObject *self, PyObject *args)
{
    SelectionSummaryObject *obj = (SelectionSummaryObject *)self;

    PyObject *packaged_args = 0;

    // Handle args packaged into a tuple of size one
    // if we think the unpackaged args matches our needs
    if (PySequence_Check(args) && PySequence_Size(args) == 1)
    {
        packaged_args = PySequence_GetItem(args, 0);
        if (PyNumber_Check(packaged_args))
            args = packaged_args;
    }

    if (PySequence_Check(args))
    {
        Py_XDECREF(packaged_args);
        return PyErr_Format(PyExc_TypeError, "expecting a single number arg");
    }

    if (!PyNumber_Check(args))
    {
        Py_XDECREF(packaged_args);
        return PyErr_Format(PyExc_TypeError, "arg is not a number type");
    }

    long val = PyLong_AsLong(args);
    int cval = int(val);

    if (val == -1 && PyErr_Occurred())
    {
        Py_XDECREF(packaged_args);
        PyErr_Clear();
        return PyErr_Format(PyExc_TypeError, "arg not interpretable as C++ int");
    }
    if (fabs(double(val))>1.5E-7 && fabs((double(long(cval))-double(val))/double(val))>1.5E-7)
    {
        Py_XDECREF(packaged_args);
        return PyErr_Format(PyExc_ValueError, "arg not interpretable as C++ int");
    }

    Py_XDECREF(packaged_args);

    // Set the totalCellCount in the object.
    obj->data->SetTotalCellCount(cval);

    Py_INCREF(Py_None);
    return Py_None;
}

/*static*/ PyObject *
SelectionSummary_GetTotalCellCount(PyObject *self, PyObject *args)
{
    SelectionSummaryObject *obj = (SelectionSummaryObject *)self;
    PyObject *retval = PyInt_FromLong(long(obj->data->GetTotalCellCount()));
    return retval;
}

/*static*/ PyObject *
SelectionSummary_SetHistogramValues(PyObject *self, PyObject *args)
{
    SelectionSummaryObject *obj = (SelectionSummaryObject *)self;

    doubleVector vec;

    if (PyNumber_Check(args))
    {
        double val = PyFloat_AsDouble(args);
        double cval = double(val);
        if (val == -1 && PyErr_Occurred())
        {
            PyErr_Clear();
            return PyErr_Format(PyExc_TypeError, "number not interpretable as C++ double");
        }
        if (fabs(double(val))>1.5E-7 && fabs((double(double(cval))-double(val))/double(val))>1.5E-7)
            return PyErr_Format(PyExc_ValueError, "number not interpretable as C++ double");
        vec.resize(1);
        vec[0] = cval;
    }
    else if (PySequence_Check(args) && !PyUnicode_Check(args))
    {
        vec.resize(PySequence_Size(args));
        for (Py_ssize_t i = 0; i < PySequence_Size(args); i++)
        {
            PyObject *item = PySequence_GetItem(args, i);

            if (!PyNumber_Check(item))
            {
                Py_DECREF(item);
                return PyErr_Format(PyExc_TypeError, "arg %d is not a number type", (int) i);
            }

            double val = PyFloat_AsDouble(item);
            double cval = double(val);

            if (val == -1 && PyErr_Occurred())
            {
                Py_DECREF(item);
                PyErr_Clear();
                return PyErr_Format(PyExc_TypeError, "arg %d not interpretable as C++ double", (int) i);
            }
            if (fabs(double(val))>1.5E-7 && fabs((double(double(cval))-double(val))/double(val))>1.5E-7)
            {
                Py_DECREF(item);
                return PyErr_Format(PyExc_ValueError, "arg %d not interpretable as C++ double", (int) i);
            }
            Py_DECREF(item);

            vec[i] = cval;
        }
    }
    else
        return PyErr_Format(PyExc_TypeError, "arg(s) must be one or more doubles");

    obj->data->GetHistogramValues() = vec;
    // Mark the histogramValues in the object as modified.
    obj->data->SelectHistogramValues();

    Py_INCREF(Py_None);
    return Py_None;
}

/*static*/ PyObject *
SelectionSummary_GetHistogramValues(PyObject *self, PyObject *args)
{
    SelectionSummaryObject *obj = (SelectionSummaryObject *)self;
    // Allocate a tuple the with enough entries to hold the histogramValues.
    const doubleVector &histogramValues = obj->data->GetHistogramValues();
    PyObject *retval = PyTuple_New(histogramValues.size());
    for(size_t i = 0; i < histogramValues.size(); ++i)
        PyTuple_SET_ITEM(retval, i, PyFloat_FromDouble(histogramValues[i]));
    return retval;
}

/*static*/ PyObject *
SelectionSummary_SetHistogramMinBin(PyObject *self, PyObject *args)
{
    SelectionSummaryObject *obj = (SelectionSummaryObject *)self;

    PyObject *packaged_args = 0;

    // Handle args packaged into a tuple of size one
    // if we think the unpackaged args matches our needs
    if (PySequence_Check(args) && PySequence_Size(args) == 1)
    {
        packaged_args = PySequence_GetItem(args, 0);
        if (PyNumber_Check(packaged_args))
            args = packaged_args;
    }

    if (PySequence_Check(args))
    {
        Py_XDECREF(packaged_args);
        return PyErr_Format(PyExc_TypeError, "expecting a single number arg");
    }

    if (!PyNumber_Check(args))
    {
        Py_XDECREF(packaged_args);
        return PyErr_Format(PyExc_TypeError, "arg is not a number type");
    }

    double val = PyFloat_AsDouble(args);
    double cval = double(val);

    if (val == -1 && PyErr_Occurred())
    {
        Py_XDECREF(packaged_args);
        PyErr_Clear();
        return PyErr_Format(PyExc_TypeError, "arg not interpretable as C++ double");
    }
    if (fabs(double(val))>1.5E-7 && fabs((double(double(cval))-double(val))/double(val))>1.5E-7)
    {
        Py_XDECREF(packaged_args);
        return PyErr_Format(PyExc_ValueError, "arg not interpretable as C++ double");
    }

    Py_XDECREF(packaged_args);

    // Set the histogramMinBin in the object.
    obj->data->SetHistogramMinBin(cval);

    Py_INCREF(Py_None);
    return Py_None;
}

/*static*/ PyObject *
SelectionSummary_GetHistogramMinBin(PyObject *self, PyObject *args)
{
    SelectionSummaryObject *obj = (SelectionSummaryObject *)self;
    PyObject *retval = PyFloat_FromDouble(obj->data->GetHistogramMinBin());
    return retval;
}

/*static*/ PyObject *
SelectionSummary_SetHistogramMaxBin(PyObject *self, PyObject *args)
{
    SelectionSummaryObject *obj = (SelectionSummaryObject *)self;

    PyObject *packaged_args = 0;

    // Handle args packaged into a tuple of size one
    // if we think the unpackaged args matches our needs
    if (PySequence_Check(args) && PySequence_Size(args) == 1)
    {
        packaged_args = PySequence_GetItem(args, 0);
        if (PyNumber_Check(packaged_args))
            args = packaged_args;
    }

    if (PySequence_Check(args))
    {
        Py_XDECREF(packaged_args);
        return PyErr_Format(PyExc_TypeError, "expecting a single number arg");
    }

    if (!PyNumber_Check(args))
    {
        Py_XDECREF(packaged_args);
        return PyErr_Format(PyExc_TypeError, "arg is not a number type");
    }

    double val = PyFloat_AsDouble(args);
    double cval = double(val);

    if (val == -1 && PyErr_Occurred())
    {
        Py_XDECREF(packaged_args);
        PyErr_Clear();
        return PyErr_Format(PyExc_TypeError, "arg not interpretable as C++ double");
    }
    if (fabs(double(val))>1.5E-7 && fabs((double(double(cval))-double(val))/double(val))>1.5E-7)
    {
        Py_XDECREF(packaged_args);
        return PyErr_Format(PyExc_ValueError, "arg not interpretable as C++ double");
    }

    Py_XDECREF(packaged_args);

    // Set the histogramMaxBin in the object.
    obj->data->SetHistogramMaxBin(cval);

    Py_INCREF(Py_None);
    return Py_None;
}

/*static*/ PyObject *
SelectionSummary_GetHistogramMaxBin(PyObject *self, PyObject *args)
{
    SelectionSummaryObject *obj = (SelectionSummaryObject *)self;
    PyObject *retval = PyFloat_FromDouble(obj->data->GetHistogramMaxBin());
    return retval;
}



PyMethodDef PySelectionSummary_methods[SELECTIONSUMMARY_NMETH] = {
    {"Notify", SelectionSummary_Notify, METH_VARARGS},
    {"SetName", SelectionSummary_SetName, METH_VARARGS},
    {"GetName", SelectionSummary_GetName, METH_VARARGS},
    {"GetVariables", SelectionSummary_GetVariables, METH_VARARGS},
    {"GetNumVariables", SelectionSummary_GetNumVariables, METH_VARARGS},
    {"AddVariables", SelectionSummary_AddVariables, METH_VARARGS},
    {"RemoveVariables", SelectionSummary_RemoveVariables, METH_VARARGS},
    {"ClearVariables", SelectionSummary_ClearVariables, METH_VARARGS},
    {"SetCellCount", SelectionSummary_SetCellCount, METH_VARARGS},
    {"GetCellCount", SelectionSummary_GetCellCount, METH_VARARGS},
    {"SetTotalCellCount", SelectionSummary_SetTotalCellCount, METH_VARARGS},
    {"GetTotalCellCount", SelectionSummary_GetTotalCellCount, METH_VARARGS},
    {"SetHistogramValues", SelectionSummary_SetHistogramValues, METH_VARARGS},
    {"GetHistogramValues", SelectionSummary_GetHistogramValues, METH_VARARGS},
    {"SetHistogramMinBin", SelectionSummary_SetHistogramMinBin, METH_VARARGS},
    {"GetHistogramMinBin", SelectionSummary_GetHistogramMinBin, METH_VARARGS},
    {"SetHistogramMaxBin", SelectionSummary_SetHistogramMaxBin, METH_VARARGS},
    {"GetHistogramMaxBin", SelectionSummary_GetHistogramMaxBin, METH_VARARGS},
    {NULL, NULL}
};

//
// Type functions
//

static void
SelectionSummary_dealloc(PyObject *v)
{
   SelectionSummaryObject *obj = (SelectionSummaryObject *)v;
   if(obj->parent != 0)
       Py_DECREF(obj->parent);
   if(obj->owns)
       delete obj->data;
}

static PyObject *SelectionSummary_richcompare(PyObject *self, PyObject *other, int op);
PyObject *
PySelectionSummary_getattr(PyObject *self, char *name)
{
    if(strcmp(name, "name") == 0)
        return SelectionSummary_GetName(self, NULL);
    if(strcmp(name, "variables") == 0)
        return SelectionSummary_GetVariables(self, NULL);
    if(strcmp(name, "cellCount") == 0)
        return SelectionSummary_GetCellCount(self, NULL);
    if(strcmp(name, "totalCellCount") == 0)
        return SelectionSummary_GetTotalCellCount(self, NULL);
    if(strcmp(name, "histogramValues") == 0)
        return SelectionSummary_GetHistogramValues(self, NULL);
    if(strcmp(name, "histogramMinBin") == 0)
        return SelectionSummary_GetHistogramMinBin(self, NULL);
    if(strcmp(name, "histogramMaxBin") == 0)
        return SelectionSummary_GetHistogramMaxBin(self, NULL);


    // Add a __dict__ answer so that dir() works
    if (!strcmp(name, "__dict__"))
    {
        PyObject *result = PyDict_New();
        for (int i = 0; PySelectionSummary_methods[i].ml_meth; i++)
            PyDict_SetItem(result,
                PyString_FromString(PySelectionSummary_methods[i].ml_name),
                PyString_FromString(PySelectionSummary_methods[i].ml_name));
        return result;
    }

    return Py_FindMethod(PySelectionSummary_methods, self, name);
}

int
PySelectionSummary_setattr(PyObject *self, char *name, PyObject *args)
{
    PyObject NULL_PY_OBJ;
    PyObject *obj = &NULL_PY_OBJ;

    if(strcmp(name, "name") == 0)
        obj = SelectionSummary_SetName(self, args);
    else if(strcmp(name, "cellCount") == 0)
        obj = SelectionSummary_SetCellCount(self, args);
    else if(strcmp(name, "totalCellCount") == 0)
        obj = SelectionSummary_SetTotalCellCount(self, args);
    else if(strcmp(name, "histogramValues") == 0)
        obj = SelectionSummary_SetHistogramValues(self, args);
    else if(strcmp(name, "histogramMinBin") == 0)
        obj = SelectionSummary_SetHistogramMinBin(self, args);
    else if(strcmp(name, "histogramMaxBin") == 0)
        obj = SelectionSummary_SetHistogramMaxBin(self, args);

    if (obj != NULL && obj != &NULL_PY_OBJ)
        Py_DECREF(obj);

    if (obj == &NULL_PY_OBJ)
    {
        obj = NULL;
        PyErr_Format(PyExc_NameError, "name '%s' is not defined", name);
    }
    else if (obj == NULL && !PyErr_Occurred())
        PyErr_Format(PyExc_RuntimeError, "unknown problem with '%s'", name);

    return (obj != NULL) ? 0 : -1;
}

static int
SelectionSummary_print(PyObject *v, FILE *fp, int flags)
{
    SelectionSummaryObject *obj = (SelectionSummaryObject *)v;
    fprintf(fp, "%s", PySelectionSummary_ToString(obj->data, "",false).c_str());
    return 0;
}

PyObject *
SelectionSummary_str(PyObject *v)
{
    SelectionSummaryObject *obj = (SelectionSummaryObject *)v;
    return PyString_FromString(PySelectionSummary_ToString(obj->data,"", false).c_str());
}

//
// The doc string for the class.
//
#if PY_MAJOR_VERSION > 2 || (PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION >= 5)
static const char *SelectionSummary_Purpose = "Contains attributes for a selection";
#else
static char *SelectionSummary_Purpose = "Contains attributes for a selection";
#endif

//
// Python Type Struct Def Macro from Py2and3Support.h
//
//         VISIT_PY_TYPE_OBJ( VPY_TYPE,
//                            VPY_NAME,
//                            VPY_OBJECT,
//                            VPY_DEALLOC,
//                            VPY_PRINT,
//                            VPY_GETATTR,
//                            VPY_SETATTR,
//                            VPY_STR,
//                            VPY_PURPOSE,
//                            VPY_RICHCOMP,
//                            VPY_AS_NUMBER)

//
// The type description structure
//

VISIT_PY_TYPE_OBJ(SelectionSummaryType,         \
                  "SelectionSummary",           \
                  SelectionSummaryObject,       \
                  SelectionSummary_dealloc,     \
                  SelectionSummary_print,       \
                  PySelectionSummary_getattr,   \
                  PySelectionSummary_setattr,   \
                  SelectionSummary_str,         \
                  SelectionSummary_Purpose,     \
                  SelectionSummary_richcompare, \
                  0); /* as_number*/

//
// Helper function for comparing.
//
static PyObject *
SelectionSummary_richcompare(PyObject *self, PyObject *other, int op)
{
    // only compare against the same type 
    if ( Py_TYPE(self) != &SelectionSummaryType
         || Py_TYPE(other) != &SelectionSummaryType)
    {
        Py_INCREF(Py_NotImplemented);
        return Py_NotImplemented;
    }

    PyObject *res = NULL;
    SelectionSummary *a = ((SelectionSummaryObject *)self)->data;
    SelectionSummary *b = ((SelectionSummaryObject *)other)->data;

    switch (op)
    {
       case Py_EQ:
           res = (*a == *b) ? Py_True : Py_False;
           break;
       case Py_NE:
           res = (*a != *b) ? Py_True : Py_False;
           break;
       default:
           res = Py_NotImplemented;
           break;
    }

    Py_INCREF(res);
    return res;
}

//
// Helper functions for object allocation.
//

static SelectionSummary *defaultAtts = 0;
static SelectionSummary *currentAtts = 0;

static PyObject *
NewSelectionSummary(int useCurrent)
{
    SelectionSummaryObject *newObject;
    newObject = PyObject_NEW(SelectionSummaryObject, &SelectionSummaryType);
    if(newObject == NULL)
        return NULL;
    if(useCurrent && currentAtts != 0)
        newObject->data = new SelectionSummary(*currentAtts);
    else if(defaultAtts != 0)
        newObject->data = new SelectionSummary(*defaultAtts);
    else
        newObject->data = new SelectionSummary;
    newObject->owns = true;
    newObject->parent = 0;
    return (PyObject *)newObject;
}

static PyObject *
WrapSelectionSummary(const SelectionSummary *attr)
{
    SelectionSummaryObject *newObject;
    newObject = PyObject_NEW(SelectionSummaryObject, &SelectionSummaryType);
    if(newObject == NULL)
        return NULL;
    newObject->data = (SelectionSummary *)attr;
    newObject->owns = false;
    newObject->parent = 0;
    return (PyObject *)newObject;
}

///////////////////////////////////////////////////////////////////////////////
//
// Interface that is exposed to the VisIt module.
//
///////////////////////////////////////////////////////////////////////////////

PyObject *
SelectionSummary_new(PyObject *self, PyObject *args)
{
    int useCurrent = 0;
    if (!PyArg_ParseTuple(args, "i", &useCurrent))
    {
        if (!PyArg_ParseTuple(args, ""))
            return NULL;
        else
            PyErr_Clear();
    }

    return (PyObject *)NewSelectionSummary(useCurrent);
}

//
// Plugin method table. These methods are added to the visitmodule's methods.
//
static PyMethodDef SelectionSummaryMethods[] = {
    {"SelectionSummary", SelectionSummary_new, METH_VARARGS},
    {NULL,      NULL}        /* Sentinel */
};

static Observer *SelectionSummaryObserver = 0;

std::string
PySelectionSummary_GetLogString()
{
    std::string s("SelectionSummary = SelectionSummary()\n");
    if(currentAtts != 0)
        s += PySelectionSummary_ToString(currentAtts, "SelectionSummary.", true);
    return s;
}

static void
PySelectionSummary_CallLogRoutine(Subject *subj, void *data)
{
    typedef void (*logCallback)(const std::string &);
    logCallback cb = (logCallback)data;

    if(cb != 0)
    {
        std::string s("SelectionSummary = SelectionSummary()\n");
        s += PySelectionSummary_ToString(currentAtts, "SelectionSummary.", true);
        cb(s);
    }
}

void
PySelectionSummary_StartUp(SelectionSummary *subj, void *data)
{
    if(subj == 0)
        return;

    currentAtts = subj;
    PySelectionSummary_SetDefaults(subj);

    //
    // Create the observer that will be notified when the attributes change.
    //
    if(SelectionSummaryObserver == 0)
    {
        SelectionSummaryObserver = new ObserverToCallback(subj,
            PySelectionSummary_CallLogRoutine, (void *)data);
    }

}

void
PySelectionSummary_CloseDown()
{
    delete defaultAtts;
    defaultAtts = 0;
    delete SelectionSummaryObserver;
    SelectionSummaryObserver = 0;
}

PyMethodDef *
PySelectionSummary_GetMethodTable(int *nMethods)
{
    *nMethods = 1;
    return SelectionSummaryMethods;
}

bool
PySelectionSummary_Check(PyObject *obj)
{
    return (obj->ob_type == &SelectionSummaryType);
}

SelectionSummary *
PySelectionSummary_FromPyObject(PyObject *obj)
{
    SelectionSummaryObject *obj2 = (SelectionSummaryObject *)obj;
    return obj2->data;
}

PyObject *
PySelectionSummary_New()
{
    return NewSelectionSummary(0);
}

PyObject *
PySelectionSummary_Wrap(const SelectionSummary *attr)
{
    return WrapSelectionSummary(attr);
}

void
PySelectionSummary_SetParent(PyObject *obj, PyObject *parent)
{
    SelectionSummaryObject *obj2 = (SelectionSummaryObject *)obj;
    obj2->parent = parent;
}

void
PySelectionSummary_SetDefaults(const SelectionSummary *atts)
{
    if(defaultAtts)
        delete defaultAtts;

    defaultAtts = new SelectionSummary(*atts);
}

